/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.stu.service.impl;

import me.zhengjie.stu.domain.Stu;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.stu.repository.StuRepository;
import me.zhengjie.stu.service.StuService;
import me.zhengjie.stu.service.dto.StuDto;
import me.zhengjie.stu.service.dto.StuQueryCriteria;
import me.zhengjie.stu.service.mapstruct.StuMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://el-admin.vip
* @description 服务实现
* @author sy
* @date 2020-12-25
**/
@Service
@RequiredArgsConstructor
public class StuServiceImpl implements StuService {

    private final StuRepository stuRepository;
    private final StuMapper stuMapper;


    @Override
    public Map<String,Object> queryAll(StuQueryCriteria criteria, Pageable pageable){
        Page<Stu> page = stuRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(stuMapper::toDto));
    }

    @Override
    public List<StuDto> queryAll(StuQueryCriteria criteria){
        return stuMapper.toDto(stuRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public StuDto findById(Long id) {
        Stu stu = stuRepository.findById(id).orElseGet(Stu::new);
        ValidationUtil.isNull(stu.getId(),"Stu","id",id);
        return stuMapper.toDto(stu);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public StuDto create(Stu resources) {
        return stuMapper.toDto(stuRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(Stu resources) {
        Stu stu = stuRepository.findById(resources.getId()).orElseGet(Stu::new);
        ValidationUtil.isNull( stu.getId(),"Stu","id",resources.getId());
        stu.copy(resources);
        stuRepository.save(stu);
    }

    @Override
    public void deleteAll(Long[] ids) {
        for (Long id : ids) {
            stuRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<StuDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (StuDto stu : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("学生姓名", stu.getSname());
            map.put("性别", stu.getSex());
            map.put("年龄", stu.getAge());
            map.put("电话号码", stu.getPhone());
            map.put("密码", stu.getPassword());
            map.put("用户头像", stu.getHead());
            map.put("爱好id", stu.getHobbyid());
            map.put("个人余额", stu.getRemaining());
            map.put("课程编号", stu.getCourseid());
            map.put("个人简介", stu.getIntroduce());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}